package org.itboys.mobile.controller.weixin.meal;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.coo.entity.mongo.meal.CooMeal;
import org.itboys.mobile.dto.member.SundayWeixinMemberDto;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.weixin.meal.CooWeixinMealService;
import org.itboys.mobile.service.mysql.weixin.product.SundayWeiXinProductService;
import org.itboys.mobile.util.MemberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/1/31
 * 描述：套餐
 */
@Controller
@RequestMapping("/coo/weixin/meal")
public class CooWeixinMealController extends BaseController{
    @Autowired
    private CooWeixinMealService mealService;
    @Autowired
    private SundayWeiXinProductService productService;

    /**
     * 套餐主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){
        List<CooMeal> meals=mealService.selectMeal(request);
        //获取session中的用户信息
        SundayWeixinMemberDto member = MemberUtil.getUser(request);
        if(LongUtil.isZreo(member.getRecommendId())){
            return "/newcoo/weixin/doLogin";
        }
        model.addAttribute("meals",meals);
        return "/newcoo/weixin/meal/index";
    }

    /**
     * 套餐详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/detail")
    public String detail(
            HttpServletRequest request,
            @RequestParam(value = "id", required = true)Long  id,
            Model model){
        //获取session中的用户信息
        SundayWeixinMemberDto member = MemberUtil.getUser(request);
        if(LongUtil.isZreo(member.getRecommendId())){
            return "/newcoo/weixin/doLogin";
        }
        CooMeal meal=mealService.getById(id);
        List<SundayProduct> products=productService.selectProduct(request,id);
        model.addAttribute("products",products);
        model.addAttribute("meal",meal);
        return "/newcoo/weixin/meal/detail";
    }
}
